package com.wechat.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.wechat.config.prperties.WxPayProperties;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *
 *
 * @author
 * @date 2018/7/9
 */
@Configuration
@ConditionalOnClass(WxPayService.class)
@EnableConfigurationProperties({WxPayProperties.class})
public class WxAllConfiguration {
    @Autowired
    private WxPayProperties properties;
    @Autowired
    private WxPayConfig wxPayConfig;

    @Bean
    @ConditionalOnMissingBean
    public WxPayConfig wxPayConfig() {
        WxPayConfig wxPayConfig = new WxPayConfig();
        wxPayConfig.setAppId(this.properties.getAppId());
        wxPayConfig.setMchId(this.properties.getMchId());
        wxPayConfig.setMchKey(this.properties.getMchKey());
        wxPayConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId()));
        wxPayConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId()));
        wxPayConfig.setKeyPath(this.properties.getKeyPath());
        return wxPayConfig;
    }

    
    /**
     * 微信支付配置--支付
     * @param
     * @return
     */
    @Bean
    public WxPayService wxPayServiceApplet() {
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(wxPayConfig);
        return wxPayService;
    }

    /**
     * 微信公众号接口
     */
    @Bean
    public WxMpService wxMpService(){
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    @Bean
    public WxMpConfigStorage wxMpConfigStorage(){
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(properties.getAppId());
        wxMpConfigStorage.setSecret(properties.getSecret());
        return wxMpConfigStorage;
    }

}
